'身份证校验'
from sfz_codes import codes
from datetime import datetime


def verify_sfz(sfz: str)->bool:
    '身份证号码校验'
    # 1、将前面的身份证号码17位数分别乘以对应位置的的系数[7 9 10 5 8 4 2 1 6 3 7 9 10 5 8 4 2 ]之后累加
    # 2、累加数对11取余,余数对应["1", "0", "x", "9", "8", "7", "6", "5", "4", "3", "2"]的字符即sfz末位校验码
    if not isinstance(sfz, str):
        return False
    sfz = sfz.lower()
    if len(sfz) == 18:
        # 系数
        coefficients = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2]
        verify_code = ["1", "0", "x", "9", "8", "7", "6", "5", "4", "3", "2"]
        summary_n = 0
        for i, s in enumerate(sfz[:-1]):
            try:
                _t = int(s)
            except ValueError:
                return False
            summary_n += _t * coefficients[i]
        return verify_code[summary_n % 11] == sfz[-1]
    elif len(sfz) == 15:
        # code verify
        if sfz[:6] not in codes:
            return False
        # date verify
        try:
            datetime.strptime(sfz[6:12], '%y%m%d')
        except ValueError:
            return False
        return True
    return False
